package org.leafstar.shortlink.admin.controller;

import lombok.RequiredArgsConstructor;
import org.leafstar.shortlink.admin.common.convention.result.Result;
import org.leafstar.shortlink.admin.common.convention.result.Results;
import org.leafstar.shortlink.admin.dto.req.GroupSaveReqDTO;
import org.leafstar.shortlink.admin.dto.req.GroupSortReqDTO;
import org.leafstar.shortlink.admin.dto.req.GroupUpdateReqDTO;
import org.leafstar.shortlink.admin.dto.resp.GroupListRespDTO;
import org.leafstar.shortlink.admin.dto.resp.GroupSaveRespDTO;
import org.leafstar.shortlink.admin.service.GroupService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 短链接分组控制层
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/short-link/admin/v1/group")
public class GroupController {
    private final GroupService groupService;

    /**
     * 新增短链接分组
     * @param requestParam 分组名称
     */
    @PostMapping("")
    public Result<Void>save(@RequestBody GroupSaveReqDTO requestParam){
        groupService.saveGroup(requestParam.getName());
        return Results.success();
    }

    /**
     * 查询分组
     * @return 分组集合
     */
    @GetMapping("")
    public Result<List<GroupListRespDTO>> groupList(){
        return Results.success(groupService.groupList());
    }

    /**
     * 修改分组名称
     * @param requestParam 修改参数
     * @return 是否成功
     */
    @PutMapping("/update")
    public Result<Boolean>updateGroup(@RequestBody GroupUpdateReqDTO requestParam){
        return Results.success(groupService.updateGroup(requestParam));
    }

    /**
     * 分组删除
     * @param gid 分组标识
     * @return bool
     */
    @DeleteMapping("/delete")
    public Result<Boolean>deleteGroup(@RequestParam String gid){
        return Results.success(groupService.deleteGroup(gid));
    }

    /**
     * 分组sort更新
     * @param requestParam 要更新的分组list
     * @return bool
     */
    @PostMapping("/sort")
    public Result<Boolean>sortGroup(@RequestBody List<GroupSortReqDTO> requestParam){
        return Results.success(groupService.sortGroup(requestParam));
    }

}
